<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>IT运维</el-breadcrumb-item>
      <el-breadcrumb-item>工具管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片-->
    <el-card>
      <!--栅栏布局：操作项-->
      <el-row :gutter="20">
        <!--搜索框-->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getToolList">
            <el-button slot="append" icon="el-icon-search" @click="getToolList"></el-button>
          </el-input>
        </el-col>
        <!--添加按钮-->
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加工具</el-button>
        </el-col>
      </el-row>

      <!--数据列表-->
      <el-table :data="toolList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column prop="type" label="获取类型"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="toolStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            </el-tooltip>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeToolById(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!--添加工具对话框-->
    <el-dialog title="添加工具" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="addForm.price"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="addForm.type"></el-input>
        </el-form-item>
      </el-form>
      <!--按钮-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTool">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改工具的对话框 -->
    <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="名称">
          <el-input v-model="editForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="editForm.type"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editTool">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    // 自定义校验规则
    var checkPrice = (rule, value, callback) => {
      if (value.startsWith('¥')) {
        return callback()
      }
      callback(new Error('价格请以¥开头'))
    }

    return {
      // 查询参数
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5
      },
      toolList: [],
      total: 0,
      addDialogVisible: false,
      addForm: {
        name: '',
        price: '',
        type: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入类型', trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      editForm: {
        name: '',
        price: '',
        type: ''
      },
      editFormRules: {
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入类型', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getToolList()
  },
  methods: {
    async getToolList () {
      /*
      const { data: res } = await this.$http.get('tools', { params: this.queryInfo })
      if (res.mata.status !== 200) return this.$message.error('获取数据失败')
      this.toolList = res.data.tools
      this.total = res.data.total
      */
      this.toolList = [
        {
          id: 1,
          name: '扳手',
          price: '¥ 14',
          type: '自购',
          status: true
        },
        {
          id: 2,
          name: '起子',
          price: '¥ 20',
          type: '分配',
          status: true
        }
      ]
      this.total = 10
    },
    toolStateChange(toolInfo) {
      console.log(toolInfo.name + '状态改变啦')
      this.$message.success('更新状态成功')
    },
    handleSizeChange(pageSize) {
      this.queryInfo.pageSize = pageSize
      this.getToolList()
    },
    handleCurrentChange(pageNum) {
      this.queryInfo.pageNum = pageNum
      this.getToolList()
    },
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    addTool() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 添加工具
        /*
        const { data: res } = await this.$http.post('users', this.addForm)
        if (res.meta.status !== 201) {
          this.$message.error('添加添加工具失败！')
        }
        */
        this.$message.success('添加工具成功！')
        this.addDialogVisible = false
        this.getToolList()
      })
    },
    async showEditDialog(id) {
      /*
      const { data: res } = await this.$http.get('users/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询用户信息失败！')
      }
      */
      this.editForm = this.toolList[id - 1]
      this.editDialogVisible = true
    },
    editTool() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        /*
        const { data: res } = await this.$http.put(
          'users/' + this.editForm.id,
          {
            email: this.editForm.email,
            mobile: this.editForm.mobile
          }
        )

        if (res.meta.status !== 200) {
          return this.$message.error('更新用户信息失败！')
        }
        */
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getToolList()
        // 提示修改成功
        this.$message.success('更新工具成功！')
      })
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    async removeToolById(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该工具, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      // 如果确认删除，则返回值为字符串 confirm
      // 如果取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      /*
      const { data: res } = await this.$http.delete('users/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除用户失败！')
      }
      */
      this.$message.success('删除工具成功！')
      this.getToolList()
    }
  }
}
</script>

<style>

</style>
